package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.entity.Bill;
import com.fz.entity.Repair;
import com.fz.mapper.BillMapper;
import com.fz.service.BillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fz.vo.PayVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 存储水电费信息 服务实现类
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@Service
public  class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService {
    @Resource
    BillMapper billMapper;
    @Override
    public PayVo getUsernameById(Integer id, Long userId){
        PayVo payVo=billMapper.getUsernameById(id,userId);
        return payVo;
    }
    @Override
    public List<PayVo> setPayVoList(List<Bill> billList){
        List<PayVo> payVoList1=new ArrayList<>();
        for(Bill bill:billList){
            Long userid = Long.valueOf(bill.getUserId());
            //userId.add(userid);
            Integer id= Math.toIntExact(bill.getId());
            // Id.add(id);
            payVoList1.add(this.getUsernameById(id,userid));
        }

        return payVoList1;
    }

    @Override
    public boolean check(Long id, String field, String value)
    {
        QueryWrapper<Bill> wrapper=new QueryWrapper<>();
        wrapper.eq(field,value);
        System.out.println(id);
        if(null!=id)
        {
            wrapper.ne("id",id);
        }
        return this.count(wrapper)==0;
    }
    @Override
    public Page<Bill> getList(Long userId, Integer pageNo, Integer pageSize) {
        QueryWrapper<Bill> wrapper = new QueryWrapper<>();
        Page<Bill>billPage;
        wrapper.eq("user_id", userId)
                .orderByDesc("create_time");
        billPage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return billPage;
    }

    @Override
    public Page<Bill> showList(Integer pageNo, Integer pageSize){
        QueryWrapper<Bill> wrapper=new QueryWrapper<>();
        Page<Bill> billPage;
        wrapper.orderByDesc("create_time");
        billPage=this.page(new Page<>(pageNo,pageSize),wrapper);
        return  billPage;

    }
    @Override
    public Boolean add(Long id, String type, BigDecimal amount){
        return this.save(new Bill(id,type,amount));
    }


}
